home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / alv.sun / alv.lha / src / cst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  7.8 KB  |  280 lines

  1. /***************************************************************************/
  2. /* This program is a heavily-modified version of Phil Everson's winlev8    */
  3. /* from the ALV v2.1 suite of public domain image processing programs for  */
  4. /* Sun workstations.  It allows interactive thresholding and contrast      */
  5. /* -stretching of greyscale images by manipulating the lookup tables (LUTs)*/
  6. /* Only greyscales from 1 to 254 are permitted. The LUTs can be saved to   */
  7. /* a file for incorporation into a rasterfile header for the image.        */
  8. /*                                                                         */
  9. /*                           cst.c                                         */
  10. /*                                                                         */
  11. /* Bryan Dawson, Keele University Geography Dept (JANET: ged08@keele.seq1) */
  12. /*                                                      27/3/89            */
  13. /***************************************************************************/
  14.  
  15. #include <suntool/sunview.h>
  16. #include <suntool/canvas.h>
  17. #include <suntool/panel.h>
  18. #include <stdio.h>
  19. #include "defs.h"
  20.  
  21. #define CSTFONT "/usr/lib/fonts/fixedwidthfonts/cour.r.16"
  22. #define XSIZE 256
  23. #define YSIZE 256
  24. #define MaxY 254
  25. #define MinY 1
  26.  
  27. har           *progname;
  28.  
  29. tatic short    csticon[] =
  30. {
  31. #include "../images/icons/cst.icon"
  32. };
  33.  
  34. EFINE_ICON_FROM_IMAGE(icon, csticon);
  35.  
  36. tatic void save_proc(),getcoords(), repaint_canvas(), invert0_proc(), quit_proc(), reset_proc();
  37. nt     invert0 = 0,fcount = 0;
  38. nt     yval = 255, xval = 127, lolim = MinY, hilim = MaxY;
  39. har           *progname;
  40. FILE * lutfile;
  41. Frame       frame;
  42. anvas      canvas;
  43. anel       panel, inv_panel;
  44. ixfont        *font;
  45. ixwin         *pw, *pw2;
  46. har        cmsname[CMS_NAMESIZE];
  47. _char      red[256], ref[256];/* all LUTs the same, so red used 3 times */
  48.  
  49. #ifdef STANDALONE
  50. ain(argc, argv, envp)
  51. #else
  52. st_main(argc, argv, envp)
  53. #endif
  54.     int      argc;
  55.     char           **argv;
  56.     char           **envp;
  57.  
  58. {
  59.     int      i, j, x, y;
  60.     u_char  *pi;
  61.     char        *makethelabel();
  62.  
  63.     progname = strsave(argv[0]);
  64.     parse_profile(&argc, argv, envp);/* link with support.c */
  65.  
  66.     while ((gc = getopt(argc, argv, " ")) != EOF)
  67.         switch (gc) {
  68.         case '?':
  69.             errflag++;
  70.             break;
  71.         }
  72.  
  73.     if (errflag)
  74.         error((char *) 0, "Usage: %s:\n", progname);
  75.  
  76.     if ((font = pf_open(CSTFONT)) == NULL) {
  77.         fprintf(stderr, "%s : Cannot open %s\n", argv[0], CSTFONT);
  78.         exit(1);
  79.     }
  80.  
  81.  /* Initialise default grey scale ramp */
  82.     for (i = 0; i < 256; i++) red[i] = ref[i] = i;
  83.     red[0] = ref[0] = 255;
  84.     red[255] = ref[255] = 0;
  85.  
  86.     /* create frame and canvas */
  87.     frame = window_create(NULL, FRAME,
  88.                  FRAME_LABEL, makethelabel(),
  89.                  WIN_HEIGHT, YSIZE + 56,
  90.                  WIN_WIDTH, XSIZE + 10,
  91.                  FRAME_ICON, &icon,
  92.                  FRAME_ARGS, argc, argv,
  93.                  0);
  94.  
  95.     panel = window_create(frame, PANEL, 0);
  96.  
  97.     inv_panel = panel_create_item(panel, PANEL_CYCLE,
  98.                    PANEL_CHOICE_STRINGS, "Normal", "Invert", 0,
  99.                       PANEL_NOTIFY_PROC, invert0_proc,
  100.                       0);
  101.     panel_create_item(panel, PANEL_BUTTON,
  102.         PANEL_LABEL_IMAGE, panel_button_image(panel, "Save", 0, 0),
  103.             PANEL_NOTIFY_PROC, save_proc,
  104.             0);
  105.     panel_create_item(panel, PANEL_BUTTON,
  106.         PANEL_LABEL_IMAGE, panel_button_image(panel, "Reset", 0, 0),
  107.               PANEL_NOTIFY_PROC, reset_proc,
  108.               0);
  109.     panel_create_item(panel, PANEL_BUTTON,
  110.          PANEL_LABEL_IMAGE, panel_button_image(panel, "Quit", 0, 0),
  111.               PANEL_NOTIFY_PROC, quit_proc,
  112.               0);
  113.  
  114.     window_fit(panel);
  115.     window_set(panel, WIN_WIDTH, XSIZE, 0);
  116.  
  117.     canvas = window_create(frame, CANVAS,
  118.                   WIN_BELOW, panel,
  119.                   WIN_X, 0,
  120.                   WIN_EVENT_PROC, getcoords,
  121.                   0);
  122.  
  123.     /* get the canvas pixwin to draw into */
  124.     pw = canvas_pixwin(canvas);
  125.  
  126.     /* Set colour map segment */
  127.     pw_setcmsname(pw, "grays");
  128.     pw_putcolormap(pw, 0, 256, red, red, red);
  129.  
  130.     window_set(canvas, WIN_CONSUME_PICK_EVENTS, LOC_DRAG, 0, 0);
  131.  
  132. /* N.B. The Y origin is top left of window, not bottom left */
  133.     pw_vector(pw, lolim, MaxY, hilim, MinY, PIX_SRC, 255);
  134.     window_main_loop(frame);
  135. }
  136.  
  137. tatic void 
  138. epaint_canvas()
  139. {
  140. w_writebackground(pw,0,0,256,256,PIX_SRC);
  141. }
  142.  
  143. tatic void
  144. nvert0_proc(item, value, event)
  145.     Panel_item   item;
  146.     int      value;
  147.     Event       *event;
  148. {
  149. egister int p;
  150.     invert0 = value;
  151. or (p=0;p<256;p++) red[p] = 255 - red[p];
  152.     setcolormap();
  153. }
  154.  
  155. tatic void
  156. etcoords(canvas, event, arg)
  157.     Canvas   canvas;
  158.     Event       *event;
  159.     caddr_t  arg;
  160. {
  161.     register int q;
  162.     char *makethelabel();
  163.  
  164.     switch(event_id(event))
  165.         {
  166.     case LOC_DRAG :
  167.     case MS_MIDDLE:
  168.        repaint_canvas();         /* clear window */
  169.        yval = 255 - event_y(event); /* where's the cursor ? */
  170.        xval = event_x(event);
  171.        if (xval < lolim) xval = lolim; /* trap over/under ranges */
  172.        if (xval > hilim) xval = hilim;
  173.        pw_vector(pw,lolim,255,xval,255 - yval,PIX_SRC,255);
  174.        pw_vector(pw,xval,255 - yval,hilim,0,PIX_SRC,255);
  175.        stretchlut(lolim,MinY,xval,yval,0);
  176.        stretchlut(xval,yval,hilim,MaxY,yval);
  177.        if (invert0) for (q=1;q<255;q++) red[q] = 255 - red[q];
  178.        pw_putcolormap(pw, 0, 256, red, red, red);
  179.        break;
  180.      case MS_LEFT :
  181.        repaint_canvas();
  182.        lolim = event_x(event);
  183.        if (lolim >= hilim) lolim = hilim - 1;
  184.        pw_vector(pw,lolim,255,hilim,0,PIX_SRC,255);
  185.        stretchlut(lolim,MinY,hilim,MaxY,0);
  186.        if (invert0) for (q=1;q<255;q++) red[q] = 255 - red[q];
  187.        pw_putcolormap(pw, 0, 256, red, red, red);
  188.        window_set(frame,FRAME_LABEL,makethelabel(),0);
  189.        break;       
  190.      case MS_RIGHT :
  191.        repaint_canvas();
  192.        hilim = event_x(event);
  193.        if (hilim <= lolim) hilim = lolim + 1;
  194.        pw_vector(pw,lolim,255,hilim,0,PIX_SRC,255);
  195.        stretchlut(lolim,MinY,hilim,MaxY,0);
  196.        if (invert0) for (q=1;q<255;q++) red[q] = 255 - red[q];
  197.        pw_putcolormap(pw, 0, 256, red, red, red);
  198.        window_set(frame,FRAME_LABEL,makethelabel(),0);
  199.        break;
  200.         } /* end of switch */
  201. }
  202.  
  203. tretchlut(lox,loy,hix,hiy,base)
  204. nt lox,loy,hix,hiy,base;
  205. {
  206. nt i,rangex,rangey,temp;
  207. ouble a;
  208.  
  209.     rangex = hix - lox + 1;
  210.     rangey = hiy - loy + 1;
  211.     a = (double)rangey / (double)rangex; /* scaling factor */
  212. or (i=MinY;i<lolim;i++) red[i] = 0;
  213. or (i=hilim;i<MaxY;i++); red[i] = 255;
  214. or (i=lox;i<hix;i++)
  215.     {
  216.     temp = (int)((i - lox + 1) * a); /* offset & gain compensation */
  217.     temp += base;           /* for top of two-part stretch */
  218.     if (temp < 1) temp = 1; /* avoid exteme values - used for text */
  219.     if (temp > 254) temp = 254;
  220.     red[i] = (unsigned char)temp;
  221.     }
  222. }
  223.  
  224. etcolormap()
  225. {
  226. nt i;
  227.  
  228. /* Initialise variables used to set colour map */
  229. ed[0] = 255;
  230. ed[255] = 0;
  231. w_putcolormap(pw, 0, 256, red, red, red);
  232. }
  233.  
  234. har *
  235. akethelabel()
  236. {
  237.     static char  buf[BUFSIZ];
  238.     char        *sprintf();
  239.     return sprintf(buf, "Lolim = %3d  Hilim = %3d", lolim, hilim);
  240. }
  241.  
  242. tatic void
  243. uit_proc()
  244. {
  245.     window_set(frame, FRAME_NO_CONFIRM, TRUE, 0);
  246.     window_destroy(frame);
  247. }
  248.  
  249. tatic void
  250. eset_proc()
  251. {
  252. egister int i;
  253.  
  254.     repaint_canvas(); /* clear window & redraw line */
  255.     pw_vector(pw,lolim,MaxY,hilim,MinY,PIX_SRC,255);
  256.  
  257.     invert0 = 0; /* reset normal/invert0 toggle */
  258.     panel_set_value(inv_panel, invert0);
  259.  
  260.     for (i = 0; i < 256; i++) red[i] = i; /* plain ramp */
  261.     setcolormap();
  262.  
  263.     lolim = MinY; hilim = MaxY; /* default labels */
  264.     window_set(frame, FRAME_LABEL, makethelabel(), 0);
  265. }
  266.  
  267. tatic void
  268. ave_proc()
  269. {
  270. nt id;
  271. har fname[10];
  272.  
  273. printf(fname,"clut.%03d",++fcount);
  274. utfile = fopen(fname,"w");
  275. d = fwrite(red,1,256,lutfile);
  276. d = fwrite(red,1,256,lutfile);
  277. d = fwrite(red,1,256,lutfile);
  278. d = fclose(lutfile);
  279. }
  280.